<template>
  <el-card style="max-width: 880px">
    <template #header>
      <div class="card-header">
        <div class="card-nav">
          <span>{{ cardTitle }}</span>
          <div>
            <el-button @click="todos.handleAdd()">新增</el-button>
            <el-button @click="todos.filter = 'all'">全部</el-button>
            <el-button @click="todos.filter = 'unfinished'">未完成</el-button>
            <el-button @click="todos.filter = 'finished'">已完成</el-button>
          </div>
        </div>
      </div>
    </template>
    <el-table :data="filteredTodos" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column prop="id" label="#" width="180" />
      <el-table-column prop="text" label="工作内容" width="180" />
      <el-table-column prop="isFinished" label="完成状态" />
      <el-table-column label="Operations">
        <template #default="scope">
          <el-button size="small" @click="handleFinis(scope.$index, scope.row)">
            确认完成
          </el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"> Edit </el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>Footer content</template>
  </el-card>
</template>

<script setup>
import { useTodos } from '@/stores/todos.js'
import { storeToRefs } from 'pinia'
const todos = useTodos()
const { filteredTodos, cardTitle } = storeToRefs(todos)

const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 1) {
    return 'warning-row'
  } else {
    return 'success-row'
  }
}

const handleFinis = (index, row) => {
  todos.handleFinis(index, row)
}

const handleEdit = (index, row) => {
  todos.handleEdit(index, row)

  console.log(index, row)
}
const handleDelete = (index) => {
  todos.delete(index)
}
</script>

<style lang="scss" scoped>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
.el-card {
  .card-header {
    .card-nav {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
}
</style>
